package com.glory.gloryquickui.view

import android.content.Context
import android.view.View
import android.widget.SeekBar
import android.widget.TextView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.viewmodel.QuickUIViewModel

/**
 *滑条组件
 */
class SeekViewController(context: Context?, val viewModel: QuickUIViewModel) : ViewController<FieldBean?>(context) {
    private var tvSeekName: TextView? = null
    private var seekBar: SeekBar? = null
    private var tvSeekValue: TextView? = null

    override fun resLayoutId(): Int {
        return R.layout.layout_seek_view
    }

    override fun onBindView(data: FieldBean?) {
        tvSeekName!!.text = data?.config?.label
        seekBar!!.max = data!!.max
        seekBar!!.progress = (data.config.defaultValue as Double).toInt()
        tvSeekValue!!.text = (data.config.defaultValue as Double).toInt().toString()
        seekBar!!.incrementProgressBy(data.step)
        //设置禁用
        seekBar?.isEnabled = !data.disabled
        seekBar!!.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                var process = i
                process /= data.step
                process *= data.step
                tvSeekValue!!.text = process.toString()
                data.config.defaultValue = process
                val map = viewModel.pageData.value!!.toMutableMap()
                map[data!!.vModel] = process.toString()
                viewModel.pageData.value = map
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })
    }

    override fun onCreatedView(view: View?) {
        tvSeekName = view?.findViewById(R.id.tvSeekName)
        seekBar = view?.findViewById(R.id.seekBar)
        tvSeekValue = view?.findViewById(R.id.tvSeekValue)
    }

    override fun masterPart(): View? {
        return seekBar
    }
}